﻿@model LanguageSelectorModel
@if (Model.AvailableLanguages.Count > 1)
{
    using (Ajax.BeginForm("LanguageSelected", "Common", null, new AjaxOptions { OnComplete = "onLanguageChangedRequestCompleted();" }, new { id = "form-languageselector" }))
    {
        var languages = Model.AvailableLanguages.Select(x => new SelectListItem
        {
            Text = x.Name,
            Value = x.Id.ToString(),
            Selected = x.Id.Equals(Model.CurrentLanguage.Id)
        });
    
        @Html.DropDownList("customerlanguage", languages)
    }
    <script type="text/javascript">
        $(document).ready(function () {
            $("#customerlanguage").change(onLanguageChanged);
        });

        function onLanguageChanged() {
            $("#form-languageselector").submit();
        }
        function onLanguageChangedRequestCompleted() {
            location.reload();
        }
    </script>
}